﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Reflection;

namespace adb
{
    /// <summary>
    /// dtu数据格式
    /// </summary>
    public class DtuDataFormat<T>
    {
        /// <summary>
        /// 设备数据
        /// </summary>
        public List<DtuData<T>> devices { get; set; }

    }
    public class DtuDataFormat {
        /// <summary>
        /// 获取数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="model"></param>
        /// <returns></returns>
        public static object[] GetModel(object model)
        {
            if (model == null) return null;
            var p = model.GetType().GetProperty("devices");
            if (p != null)
            {
                var lst = p.GetValue(model);
                if (lst != null && lst is IEnumerable<object> ll)
                {
                    List<object> lll = new List<object>();
                    PropertyInfo info = null;
                    foreach (var item in ll)
                    {
                        if (info == null) {
                           info = item.GetType().GetProperty("deviceData");
                        }
                        if (info != null) {
                            lll.Add(info.GetValue(item));
                        }
                    }
                    return lll.ToArray();

                }
            }
            return null;//model.devices.Select(x=>x.deviceData).ToArray();
        }
    }

    /// <summary>
    /// dtu数据
    /// </summary>
    public class DtuData<T> { 
        /// <summary>
        /// 设备ID
        /// </summary>
        public string deviceId { get; set; }
        /// <summary>
        /// 数据状态
        /// </summary>
        public int deviceState { get; set; }
        /// <summary>
        /// 数据
        /// </summary>
        public T deviceData { get; set; }
    }
}
